package com.qf.electronic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.aspect.Operation;
import com.qf.electronic.common.R;
import com.qf.electronic.common.ResponseStatus;
import com.qf.electronic.dto.DefectTaskReceptionDto;
import com.qf.electronic.dto.DefectTaskReceptionStateDto;
import com.qf.electronic.dto.InspectionReceptionStateDto;
import com.qf.electronic.pojo.DefectTaskReception;
import com.qf.electronic.pojo.User;
import com.qf.electronic.service.IDefectTaskReceptionService;
import com.qf.electronic.vo.DefectTaskReceptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/defectTaskReception")
public class DefectTaskReceptionController {


    @Autowired
    private IDefectTaskReceptionService taskReceptionService;

    @PostMapping
    @Operation("添加消缺任务回执")
    public R<Boolean> addDefectTaskReception(@Valid @RequestBody DefectTaskReception taskReception, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        taskReception.setOperationUser(user.getUsername());
        return R.ok(taskReceptionService.save(taskReception));
    }

    @GetMapping
    @Operation("查询消缺任务回执")
    public R<Page<DefectTaskReceptionVo>> searchDefectTaskReceptions(DefectTaskReceptionDto receptionDto){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        Integer roleId = user.getRoleId();
        if(roleId == 2){
            receptionDto.setRole("ROLE_LINE"); //线路管理员可以查询所有提交的回执
        } else if(roleId == 4){//巡检员只能查询自己的回执
            receptionDto.setRole("ROLE_DEFECT");
            receptionDto.setOwner(user.getUsername());
        }
        return R.ok(taskReceptionService.getDefectTaskReceptions(receptionDto));
    }


    @PutMapping
    @Operation("更新消缺任务回执")
    public R<Boolean> updateDefectTaskReception(@Valid @RequestBody DefectTaskReception taskReception, BindingResult result){
        if(result.hasFieldErrors()){
            return R.error(ResponseStatus.INVALID_PARAMETER);
        }
        Integer state = taskReception.getState();
        if(state > 0){
            return R.error(ResponseStatus.ILLEGAL_DATA_STATE);
        }
        return R.ok(taskReceptionService.updateById(taskReception));
    }

    @PutMapping("/state")
    @PreAuthorize("hasAnyRole('ROLE_LINE', 'ROLE_DEFECT')")
    @Operation("更改消缺任务回执状态")
    public R<Boolean> changeDefectTaskReceptionState(@RequestBody DefectTaskReceptionStateDto stateDto){
        //当状态修改为2的时候，表示审核通过，审核通过可能会差生消缺任务，同时，巡检任务的状态应该更改为已完成
        return R.ok(taskReceptionService.changeDefectTaskReceptionState(stateDto));
    }
}

